import fetch from 'utils/fetch'

// 获取资源一级目录
export function getTop() {
  return fetch({
    url: '/source/top/cat/',
    method: 'get'
  })
}

// 获取下级分类
export function getChild(data) {
  return fetch({
    url: '/source/cat/child/',
    method: 'get',
    params: data
  })
}

// 获取资源列表
export function getSource(data) {
  return fetch({
    url: '/source/list/',
    method: 'get',
    params: data
  })
}

// 搜索资源
export function searchSource(data) {
  return fetch({
    url: '/source/search/',
    method: 'get',
    params: data
  })
}

// 预览资源
export function preview(data) {
  return fetch({
    url: '/source/preview/',
    method: 'get',
    params: data
  })
}

// 上传
export function upload(data) {
  return fetch({
    url: '/source/upload/',
    method: 'post',
    data,
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      const form = new FormData()
      for (const key in data) {
        form.append(key, data[key])
      }
      return form
    }],
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 下载
export function download(data) {
  return fetch({
    url: '/source/download/view/',
    method: 'get',
    params: data
  })
}

// 处理下载
export function getFile(data) {
  return fetch({
    url: '/source/download/do/',
    method: 'get',
    params: data
  })
}

// 获取试卷
export function getPapers(data) {
  return fetch({
    url: '/paper/list/',
    method: 'get',
    params: data
  })
}

// 获取微库文章
export function getWechat(data) {
  return fetch({
    url: '/source/wechat/',
    method: 'get',
    params: data
  })
}

// 收藏资源
export function collect(data) {
  return fetch({
    url: '/common/collect/',
    method: 'post',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function(data) {
      // Do whatever you want to transform the data
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    data
  })
}
